//-----------------------------------------------------------------------------
// Copyright (c) 2011 David Waby
// Initially created on 03/11/2011
//-----------------------------------------------------------------------------

#include "Global.h"
#include "EventManager.h"
#include "IEventListener.h"

template<> EventManager* Singleton<EventManager>::ms_instance = NULL;

//-----------------------------------------------------------------------------
EventManager::EventManager()
{
	m_listeners.reserve(10);
} 

//-----------------------------------------------------------------------------
EventManager::~EventManager()
{
} 

//-----------------------------------------------------------------------------
void EventManager::AddListener(IEventListener * i_eventListener)
{
	m_listeners.push_back(i_eventListener);
}

//-----------------------------------------------------------------------------
void EventManager::RemoveListener(IEventListener * i_eventListener)
{
	IEventListener * listener = NULL;
	for (ListenerIterator iterator = m_listeners.begin(); iterator != m_listeners.end(); ++iterator)
	{
		listener = (*iterator);
		if (listener == i_eventListener)
		{
			m_listeners.erase(iterator);
			return;
		}
	}
}

//-----------------------------------------------------------------------------
void EventManager::PostEvent(Event const & i_event)
{
	IEventListener * listener = NULL;
	for (ListenerIterator iterator = m_listeners.begin(); iterator != m_listeners.end(); ++iterator)
	{
		listener = (*iterator);
		listener->HandleEvent(i_event);
	}
}